Test Series - Data Structure

Test Number 24/115

Q: How many stacks are required for evaluation of prefix expression?
A. one
B. two
C. three
D. four
Solution: 2 stacks are required for evaluation of prefix expression, one for integers and one for characters.
Q: While evaluating a prefix expression, the string is read from?
A. left to right
B. right to left
C. center to right
D. center to left to right
Solution: The string is read from right to left because a prefix string has operands to its right side.
Q: The associativity of an exponentiation operator ^ is right side.
A. True
B. False
C. ....
D. ....
Solution: The associativity of ^ is right side while the rest of the operators like +,-,*,/ has its associativity to its left.
Q: How many types of input characters are accepted by this algorithm?
A. one
B. two
C. three
D. four
Solution: Three kinds of input are accepted by this algorithm- numbers, operators and new line characters.
Q: What determines the order of evaluation of a prefix expression?
A. precedence and associativity
B. precedence only
C. associativity only
D. depends on the parser
Solution: Precedence is a very important factor in determining the order of evaluation. If two operators have the same precedence, associativity comes into action.
Q: Find the output of the following prefix expression.

*+2-2 1/-4 2+-5 3 1
A. 2
B. 12
C. 10
D. 4
Solution: The given prefix expression is evaluated using two stacks and the value is given by (2+2-1)*(4-2)/(5-3+1)= 2.
Q: An error is thrown if the character ‘
’ is pushed in to the character stack.
A. true
B. false
C. ...
D. ...
Solution: The input character ‘
’ is accepted as a character by the evaluation of prefix expression algorithm.
Q: Using the evaluation of prefix algorithm, evaluate +-9 2 7.
A. 10
B. 4
C. 17
D. 14
Solution: Using the evaluation of prefix algorithm, +-9 2 7 is evaluated as 9-2+7=14.
Q: If -*+abcd = 11, find a, b, c, d using evaluation of prefix algorithm.
A. a=2, b=3, c=5, d=4
B. a=1, b=2, c=5, d=4
C. a=5, b=4, c=7,d=5
D. a=1, b=2, c=3, d=4
Solution: The given prefix expression is evaluated as ((1+2)*5)-4 = 11 while a=1, b=2, c=5, d=4.
Q: In the given C snippet, find the statement number that has error.

//C code to push an element into a stack
1. void push( struct stack *s, int x) 
2. {
3.     if(s->top==MAX-1)
4.     {
5.         printf(“stack overflow”);
6.     }
7.     else
8.     {
9.         s->items[++s->top]=x;
10.        s++;
11.    }   
12. }
A. 1
B. 9
C. 10
D. 11
Solution: If the stack is not full then we are correctly incrementing the top of the stack by doing “++s->top” and storing the value of x in it. However, in the next statement “s++”, we are un-necessarily incrementing the stack base pointer which will lead to memory corruption during the next push() operation.

You Have Score    /10